package cn.jsl.xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 异步消息生产者
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("xhr-producerGroup");
        //设置nameserver地址
        producer.setNamesrvAddr("localhost:9876");
        //启动生产者
        producer.start();
        //创建消息
        Message message = new Message("xhr", "one", "今天学MQ,真开心".getBytes(StandardCharsets.UTF_8));
        //执行发送
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("异步消息发送成功:"+sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("异步消息发送失败!");
                throwable.printStackTrace();
            }
        });
        //关闭-异步不需要关闭生产者 关闭了会接收不到回调的消息
//        producer.shutdown();
    }
}
